﻿$(document).ready(function () {
    var url = new Url();
    var $css = url.GetQueryString("css");
    $.ajax({
        type: "post",
        url: "../Handler/FinancialProductOrderEmergentHandler.ashx",
        dataType: "json",
        data: { type: 2 },
        async: false,
        cache: true,
        success: function (result) {
            if (result.Status) {
                var str = '<option value="">请选择</option>';
                $(result.Data).each(function (index, item) {
                    str += '<option value="' + result.Data[index].ProductId + '">' + result.Data[index].ProductName + '</option>';
                });
                $(".productTypeId").html(str);
            } else {
                alert(result.Message);
            }
        }
    });
    //设置分页信息
    var page = new Page();
    page.Url = "../Handler/FinancialProductOrderEmergentHandler.ashx";
    page.Dom = $("#searchArea");
    page.type = 1;
    page.CallBack = function (data) {
        $(".borrowList").empty();
        $.each(data, function (index, item) {
            item.Css = $css;
            item.Payable = item.Payable ? "inline" : "none";
            $("#ContractTemplete").tmpl(item).appendTo($(".borrowList"));
        });
        $(".btnRedemption").bind("click", function () {
            var actualamount = parseFloat($(this).attr("data-actualamount"))
            GetOrderDetails($(this).attr("data-id"), (Math.round(actualamount * 100) / 100 + "元"));
        });
    };
    page.GetPageData();
    //搜索用户
    $(".btnSearchBorrow").bind("click", function () {
        var $userName = $(".txtName").val();
        var $phone = $(".txtPhone").val();
        $.getJSON("../Handler/FinancialUser.ashx",
            { UserName: $userName, Phone: $phone, type: 11, _: Math.random() },
            function (result) {
                $(".userInfo").empty();
                if (result.Status) {
                    $(result.Data).each(function (index, item) {
                        $("#UserTemplete").tmpl(item).appendTo($(".userInfo"));
                    });
                    $(".userInfo tr").css("cursor", "pointer").click(function () {
                        $(".txtUserName").val($(this).find(".name").text());
                        $(".txtUserId").val($(this).find(".userId").text());
                    });
                } else {
                    alert(result.Message);
                }
            });
    });
    $(".btnDelLender").bind("click", function () {
        $(".txtUserName").val("");
        $(".txtUserId").val("");
    });
    $(".btnShow").bind("click", function () {
        $(".txtName").val($(".txtUserName").val());
    })
    $(".txtuserName").bind("click", function () {
        $(".btnShow").click();
    });

    $(".btnSendRedemption").bind("click", function () {
        var BANK_NAME = $("label[data-name='TransferBackBankName']").text();
        var ACCOUNT_NAME = $("label[data-name='TransAccName']").text();//划扣收款账户名
        var ACCOUNT_NO = $("label[data-name='TransAccNum']").text();//划扣收款账户号码
        var Dict_Id = $("input[data-name='PayBank']").val();//银行编码
        var AMOUNT = $("label[data-name='Amount']").text();//金额
        var OrderId = $("input[data-name='OrderId']").val();
        $.ajax({
            type: "post",
            url: "../Handler/FinancialProductOrderEmergentHandler.ashx",
            dataType: "json",
            data: { "type": 3, "OrderId": OrderId, "Dict_Id": Dict_Id, "ACCOUNT_NO": ACCOUNT_NO, "ACCOUNT_NAME": ACCOUNT_NAME, "AMOUNT": AMOUNT, "BANK_NAME": BANK_NAME, _: Math.random() },
            cache: false,
            success: function (result) {
                if (result.Status) {
                    if (result.Data == "0000") {
                        alert(result.Message);
                    }
                    window.location.reload();
                } else {
                    alert(result.Message);
                }
            }
        });
    });
});
function GetOrderDetails(id, actualAmount) {

    $("label[data-name='Product']").text("");
    $("label[data-name='Amount']").text("");
    $("label[data-name='TransferBackBankName']").text("");//划扣银行编码  划扣银行
    $("label[data-name='TransOpenBank']").text("");//划扣开户行
    $("label[data-name='TransAccName']").text("");//划扣收款账户名
    $("label[data-name='TransAccNum']").text("");//划扣收款账户号码
    $("input[data-name='PayBank']").val("");//银行编号
    $("input[data-name='OrderId']").val("");
    $("label[data-name='Term']").text("");
    $("label[data-name='ActualAmount']").text("");
    $.ajax({
        type: "post",
        url: "../Handler/FinancialOrder.ashx",
        dataType: "json",
        data: { type: 4, Id: id, _: Math.random() },
        cache: true,
        success: function (result) {
            if (result.Status) {
                $("label[data-name='ActualAmount']").text(actualAmount);
                $("label[data-name='Product']").text(result.Data.Order.Product);
                $("label[data-name='Amount']").text(result.Data.Order.Amount);
                $("input[data-name='PayBank']").val(result.Data.Order.PayBank);
                $("input[data-name='OrderId']").val(id);
                $("label[data-name='Term']").text(result.Data.Order.Term);
                if (result.Data.Order.IsSameBank) {
                    $("label[data-name='TransferBackBankName']").text(result.Data.Order.PayBankName.trim());//划扣银行编码  划扣银行
                    $("label[data-name='TransOpenBank']").text(result.Data.Order.DepositBank.trim());//划扣开户行
                    $("label[data-name='TransAccName']").text(result.Data.Order.RecAccName.trim());//划扣收款账户名
                    $("label[data-name='TransAccNum']").text(result.Data.Order.RecAccNum.trim());//划扣收款账户号码
                } else {
                    $("label[data-name='TransferBackBankName']").text(result.Data.Order.TransferBackBankName.trim());//划付银行编码  划付银行
                    $("label[data-name='TransOpenBank']").text(result.Data.Order.TransOpenBank.trim());//划付开户行
                    $("label[data-name='TransAccName']").text(result.Data.Order.TransAccName.trim());//划付账户名
                    $("label[data-name='TransAccNum']").text(result.Data.Order.TransAccNum.trim());//划付账户号码
                }
            } else {
                alert(result.Message);
            }
        }
    });
}

function DateFormat(date, fmt) { //author: meizz 
    var o = {
        "M+": date.getMonth() + 1, //月份 
        "d+": date.getDate(), //日 
        "h+": date.getHours(), //小时 
        "m+": date.getMinutes(), //分 
        "s+": date.getSeconds(), //秒 
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度 
        "S": date.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

